<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 挂件基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace addons\common\widget;

use app\core\model\CacheKey;
use think\Cache;
use think\Config;
use think\Db;
use think\Env;
use think\View;

/**
 * 挂件
 *
 * @package addons\common\widget
 */
abstract class AddonsWidget {

    const TYPE_HTML = "html";
    const TYPE_NUMBER = "number";

    /**
     * 视图对象
     *
     * @var View
     */
    private $view;

    /**
     * 企业信息
     *
     * @var
     */
    protected $enterprise;

    /**
     * 模块信息
     *
     * @var
     */
    protected $module;

    /**
     * 用户信息
     *
     * @var
     */
    protected $user;

    /**
     * AddonsWidget constructor.
     * @param $enterprise
     * @param $module
     * @param $user
     */
    public function __construct($enterprise, $module, $user) {
        $this->enterprise = $enterprise;
        $this->module = $module;
        $this->user = $user;
        $templateConfig = Config::get('template');
        $this->view = new View($templateConfig, Config::get('view_replace_str'));
    }

    /**
     * 挂件标题，返回false表示没有标题
     *
     * @return mixed
     */
    public abstract function getTitle();

    /**
     * 缓存失效时间
     *
     * @return mixed
     */
    public abstract function getCacheExpire();

    /**
     * 缓存对应的key
     *
     * @return CacheKey
     */
    public function getCacheKey() {
        $widgetName = get_called_class();
        $cacheKey = CacheKey::module()->type("widget")->tags([
            'eid' => $this->enterprise['id'],
            'mid' => $this->module['id'],
            'widget' => $widgetName
        ]);
        return $cacheKey;
    }

    /**
     * 获取内容(带缓存)
     *
     * @return mixed
     */
    public function getContent() {
        if (Env::get("env") == "dev") {
            // 线下测试的时候，不进行缓存
            return $this->getContentWithoutCache();
        }
        $cacheKey = $this->getCacheKey();
        $content = $cacheKey->getValue();
        if ($content !== false) {
            return $content;
        }
        $content = $this->getContentWithoutCache();
        $cacheKey->setValue($content, $this->getCacheExpire());
        Cache::set($cacheKey->getKey() . "_time___", time());
        return $content;
    }

    /**
     * 获取不带缓存的内容
     *
     * @return mixed
     */
    public abstract function getContentWithoutCache();

    /**
     * 获取数据日期
     */
    public function getContentDataTime() {
        return Cache::get($this->getCacheKey()->getKey() . "_time___");
    }

    /**
     * 获取内容类型
     *
     * @return string
     */
    public function getContentType() {
        return self::TYPE_HTML;
    }

    /**
     * 清除缓存
     */
    public function clearCache() {
        Cache::rm($this->getCacheKey()->getKey());
    }

    /**
     * 获取模板内容
     *
     * @param $fileName
     * @param array $vars
     * @param array $replace
     * @return string
     * @throws \think\Exception
     */
    public function fetch($fileName, $vars = [], $replace = []) {
        if (!endsWith($fileName, ".html")) {
            $fileName = $fileName . ".html";
        }
        $fileName = ROOT_PATH . "addons" . DS . $this->module['name'] . DS . "widget" . DS . "view" . DS . $fileName;
        $vars['enterprise'] = $this->enterprise;
        $vars['module'] = $this->module;
        $vars['user'] = $this->user;
        return $this->view->fetch($fileName, $vars, $replace);
    }

    /**
     * 获取查询对象
     *
     * @param $name
     * @return \think\db\Query
     */
    protected function _q($name) {
        $tableName = $this->module['name'] . "_" . $name;
        return Db::name($tableName);
    }
}